define(function(require) {

    'use strict';

    var Backbone = require('backbone'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/keyword.html'),
        WxEmotion = require('components/weixin_emotion/com'),
        utils = require('core/utils');

    return Base.extend({
        template: _.template(template),
        className: 'popover bottom',
        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback',
            'keypress .js-txt': 'enterSave'
        },
        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            this.data = options.data;

            if(options.data.weixin == undefined){
                this.data.weixin = false;
            }

            if(options.data.showP === undefined) {
                this.data.showP = true
            }

            if(options.data.showQ == undefined) {
                this.data.showQ = true
            }

            var self = this;
            this.$el.delegate('.js-emotion', 'click', function(){
                self.wxEmotion.toggle();
            });
        },
        render: function() {
            var that = this;

            that.$el.html(that.template(that.data));

            that.txt = that.$el.find('.js-txt');
            that.keywordType = that.$el.find('.keyword-type-group');

            this.wxEmotion = new WxEmotion({
                target: this.$('.js-emotion'),
                el: this.$('.emotion-wrapper'),
                kind: 1
            });            
            
            this.listenTo(this.wxEmotion, 'selected:emotion', this.insertText);

            return that;
        },
        insertText: function(text){
            var input = this.$('.js-txt');
            input.val(input.val() + text);
        },
        getMode: function() {
            var that = this;

            return Number(that.keywordType.find(':checked').val());
        },
        positioning: function() {
            var that = this;

            that.$el.show().position({
                of: that.target,
                my: 'center top',
                at: 'center bottom',
                collision: 'none'
            });

            that.txt.focus();
        },
        setData: function(data) {
            this.data = _.extend(this.data, data);
            this.render();
        },
        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data;

            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();
        },
        enterSave: function(event) {
            if (event.which === utils.keyCode.ENTER) {
                this.triggerCallback();
                return false;
            }
        },
        triggerCallback: function() {
            var that = this;

            // @TODO 改造成 new Keyword model 出来去处理数据校验等操作
            var _text = $.trim(that.txt.val());
            var _type = that.getMode();

            if (utils.isEmpty(_text)) {
                that.$('.err-message').text('关键词不能为空');
                that.txt.focus();
                return false;
            } else {
                that.$('.err-message').text('');
            }

            if (_type !== 1 && _type !== 2 && _type !== 3) {
                return false;
            }

            that.setData({
                text: _text,
                type: _type
            });
            that.callback.call(that, that.data);
            that.hide();
        }
    });
});
